package com.woniuxy.service.serviceimp;

import com.fasterxml.jackson.databind.JsonNode;
import com.woniuxy.entity.*;
import com.woniuxy.mapper.*;
import com.woniuxy.service.LiuService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
@author Jan
@create 2020-05-21 17:09

*/
@Service
public class LiuServiceImp implements LiuService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductCommentMapper productCommentMapper;
    @Resource
    private JsCollectionMapper jsCollectionMapper;
    @Resource
    private JsShoppingCarMapper jsShoppingCarMapper;
    @Resource
    private UserAddressMapper userAddressMapper;
    @Resource
    private ProductOrderMapper productOrderMapper;
    @Resource
    private ProductOrderDetailMapper  productOrderDetailMapper;
    //查询所有集市商品
    @Override
    public List<Product> selectAllProduct(Map map) {
        return productMapper.selectAllProduct(map);
    }
    //查询商品详情
    @Override
    public Product selectProductByProId(Map map) {
        return productMapper.selectProductByProId(map);
    }
    //查询该proId的所有评论
    @Override
    public List<ProductComment> selectAllCommentByProId(Map map) {
        return productCommentMapper.selectAllCommentByProId(map);

    }
    //查询该商品是否收藏
    @Override
    public JsCollection judgeUserCollection(int proId, int userId) {
        JsCollectionExample example = new JsCollectionExample();
        example.createCriteria().andProidEqualTo(proId).andUserIdEqualTo(userId);
        List<JsCollection> collections = jsCollectionMapper.selectByExample(example);
        if (collections.size()!=0){
            return collections.get(0);
        }else{
            return null;
        }
    }
    //收藏
    public int AddCollection(JsCollection jsCollection) {
        return jsCollectionMapper.insert(jsCollection);
    }
    //取消收藏
    public int deleteCollection(int proId, int userId) {
        JsCollectionExample example = new JsCollectionExample();
        example.createCriteria().andUserIdEqualTo(userId).andProidEqualTo(proId);
        return jsCollectionMapper.deleteByExample(example);
    }

    //查询该用户购物车所有详情
    public List<JsShoppingCar> selectProductShoppingCarByUserId( int userId) {
        List<JsShoppingCar> list = jsShoppingCarMapper.selectProductShoppingByUserId(userId);
        return list;
    }

    //添加到购物车
    @Override
    public int insertProductShoppingCar(int userId, int proId, int number) {
        //先查询在购物车中是否存在
        JsShoppingCarExample example = new JsShoppingCarExample();
        example.createCriteria().andProIdEqualTo(proId).andUserIdEqualTo(userId);
        List<JsShoppingCar> list = jsShoppingCarMapper.selectByExample(example);
        if(list.size()==0){
            //说明不存在直接添加
            JsShoppingCar car = new JsShoppingCar();
            car.setUserId(userId);
            car.setProId(proId);
            car.setNumber(number);
            return jsShoppingCarMapper.insert(car);
        }else{
            //说明存在直接,则直接更新数量
            JsShoppingCar car = list.get(0);
            car.setNumber(car.getNumber()+number);
            //根据id更新
            return  jsShoppingCarMapper.updateByPrimaryKey(car);
        }
    }
    //查询所有用户地址
    public List<UserAddress> selectAllUserAddressByUserId(int userId) {
        UserAddressExample example = new UserAddressExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause("status");
        List<UserAddress> list = userAddressMapper.selectByExample(example);
        return list;
    }

    @Override
    public int updateuserMorenAddress(int newAddressId, int oldAddressId) {
        //先查后该
        UserAddressExample example = new UserAddressExample();
        example.createCriteria().andAddressIdEqualTo(newAddressId);
        UserAddress address1= userAddressMapper.selectByExample(example).get(0);
        address1.setStatus(0);
        int i = userAddressMapper.updateByPrimaryKey(address1);

        UserAddressExample example1 = new UserAddressExample();
        example1.createCriteria().andAddressIdEqualTo(oldAddressId);
        UserAddress address2= userAddressMapper.selectByExample(example1).get(0);
        address2.setStatus(1);
        int i1 = userAddressMapper.updateByPrimaryKey(address2);
        if(i1!=0 & i!=0){
            return 1;
        }else{
            return 0;
        }
    }

    //查询用户默认地址
    public UserAddress selectUserMorenAddress(int userId) {
        UserAddressExample example = new UserAddressExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(0);
        List<UserAddress> list = userAddressMapper.selectByExample(example);
        if(list.size()==0){
            //说明还用户暂无地址
            return null;
        }else{
            return list.get(0);
        }
    }

    //添加新地址
    public int addUserAddress(int userId, String name, String tel, String address, String ifMoRen) {
        //先查询该用户的默认地址
        //每个用户都有默认地址
        UserAddressExample example = new UserAddressExample();
        example.createCriteria().andUserIdEqualTo(userId).andStatusEqualTo(0);
        List<UserAddress> list = userAddressMapper.selectByExample(example);
        if(list.size()==0){
            //说明此人没有任何地址,不论他选的此次添加的地址是否默认,都要设置成默认
            //直接添加成默认地址
            UserAddress userAddress = new UserAddress();
            userAddress.setUserId(userId);
            userAddress.setName(name);
            userAddress.setTel(tel);
            userAddress.setAddress(address);
            userAddress.setStatus(0);
            int i = userAddressMapper.insert(userAddress);
            return i;
        }else{
            //说明此人有默认地址,对他进行此次添加地址默认与否进行判断
            if(ifMoRen.equals("0")){
                //为0时,先将之前的默认地址换成普通地址
                UserAddress oldMoRen = list.get(0);
                oldMoRen.setStatus(1);
                //更改数据库
                userAddressMapper.updateByPrimaryKey(oldMoRen);
                //再添加此次新增入的默认地址
                UserAddress userAddress = new UserAddress();
                userAddress.setUserId(userId);
                userAddress.setName(name);
                userAddress.setTel(tel);
                userAddress.setAddress(address);
                userAddress.setStatus(0);
                int i = userAddressMapper.insert(userAddress);
                return i;
            }else{
                //此次添加设置为普通地址
                UserAddress userAddress = new UserAddress();
                userAddress.setUserId(userId);
                userAddress.setName(name);
                userAddress.setTel(tel);
                userAddress.setAddress(address);
                userAddress.setStatus(1);
                int i = userAddressMapper.insert(userAddress);
                return i;
            }
        }
    }

    //根据id删除地址
    public int deleteAddress(int addressId) {
        return userAddressMapper.deleteByPrimaryKey(addressId);
    }

    //根据UserAddress对象更新地址
    public int updateUserAddress(UserAddress userAddress) {
       return userAddressMapper.updateByPrimaryKey(userAddress);
    }
    //根据对象修改购物车页面更改购物车商品数量
    public int updateShoppingCar(JsShoppingCar jsShoppingCar) {
        return jsShoppingCarMapper.updateByPrimaryKey(jsShoppingCar);
    }

    //根据id删除购物车
    public int deleteShoppingCar(int id) {
        return jsShoppingCarMapper.deleteByPrimaryKey(id);
    }

    //查询该用户所有收藏
    public List<JsCollection> selectAllCollection(int userId) {
        return jsCollectionMapper.selectAllCollection(userId);
    }

    //根据id删除收藏
    public int deleteCollectionById(int id) {
        return jsCollectionMapper.deleteByPrimaryKey(id);
    }

    //查询该用户所有订单详情userId,status(条件查询)
    public List<ProductOrder> selectAllProductOrder(int userId, int status) {
        return productOrderMapper.selectAllOrderByStatus(userId,status);
    }

    //从ProductOrder删除
    public int deleteFromProductOrder(int productOrderId) {
        ProductOrderExample example = new ProductOrderExample();
        example.createCriteria().andProOrderIdEqualTo(productOrderId);
        return productOrderMapper.deleteByExample(example);
    }

    //从ProductOrderDetail删除
    public int deleteFromProductOrderDetail(int productOrderId) {
        ProductOrderDetailExample example = new ProductOrderDetailExample();
        example.createCriteria().andProductOrderIdEqualTo(productOrderId);
        return productOrderDetailMapper.deleteByExample(example);
    }

    //改变订单状态status
    public int updateProductOrderStatus(int proOrderId, int status) {
        ProductOrder order = new ProductOrder();
        order.setProOrderId(proOrderId);
        order.setStatus(status);
        int i = productOrderMapper.updateByPrimaryKeySelective(order);
        return i;
    }

    //改变订单详情product_order_detail已评价ifcomment为1
    public int updateProductOrderDetailIfcomment(int id) {
        ProductOrderDetail detail = new ProductOrderDetail();
        detail.setId(id);
        detail.setIfcomment(1);
        return productOrderDetailMapper.updateByPrimaryKeySelective(detail);
    }

    //增加评论
    public int insertProductComment(int proId, int userId, Date time, String comment) {
        ProductComment pc = new ProductComment();
        pc.setProId(proId);
        pc.setUserId(userId);
        pc.setTime(time);
        pc.setComment(comment);
        return  productCommentMapper.insert(pc);
    }

    //用户删除订单 将product_order中的ifcomment改为0
    public int updateProductOrderIfCommentToZero(int proOrderId) {
        ProductOrder order = new ProductOrder();
        order.setProOrderId(proOrderId);
        order.setIfcomment(0);
        return productOrderMapper.updateByPrimaryKeySelective(order);
    }
    //新增订单返回主键
    @Override
    public int addProductOrderGetKey(ProductOrder productOrder) {
        return productOrderMapper.addProductOrderGetKey(productOrder);
    }
    //新增订单商品详情
    @Override
    public int addProductOrderDetail(ProductOrderDetail productOrderDetail) {
        return productOrderDetailMapper.insert(productOrderDetail);
    }
    //改变订单状态为2
    @Override
    public int updateProductOrderStatusTwo(ProductOrder productOrder) {
        return productOrderMapper.updateByPrimaryKeySelective(productOrder);
    }
    //减库存
    @Override
    public int updateProductStock(int proId, int number) {
        Product product = productMapper.selectByPrimaryKey(proId);
        if(number<=product.getStock()){
            product.setStock(product.getStock()-number);
            productMapper.updateByPrimaryKeySelective(product);
        }else{

        }
        return 1;
    }

}
